/**  
* 实现对Java配置文件Properties的读取、写入与更新操作  
*/   
package com.dabay.web.service;  
  
import java.io.BufferedInputStream;   
import java.io.FileInputStream;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;   
  
  
/**  
* @author  
* @version  
*/   
public class WxAccessTokenProperties {   
    //属性文件的路径   
    static String profilepath="F:/workspace/fhl_web_nbwx/src/main/webapp/WEB-INF/wxAccessToken.properties";   
    /**  
    * 采用静态方
    */   
    private static Properties props = new Properties();   
    static {   
        try {   
            props.load(new FileInputStream(profilepath));   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    /**  
    * 读取属性文件中相应键的
    * @param key  
    *            主键  
    * @return String  
    */   
    public static String getKeyValue(String key) {   
        return props.getProperty(key);   
    }   
  
    /**  
    * 根据主键key读取主键的值value  
    * @param filePath 属性文件路
    * @param key 键名  
    */   
    public static String getProertiesValue(String key) {   
        Properties props = new Properties();   
        try {   
            InputStream in = new BufferedInputStream(new FileInputStream(   
            		profilepath));   
            props.load(in);   
            String value = props.getProperty(key);   
            System.out.println(key +"键的值是"+ value);   
            return value;   
        } catch (Exception e) {   
            e.printStackTrace();   
            return null;   
        }   
    }   
      
    /**  
    * 更新（或插入）一对properties信息(主键及其键 
    * 如果该主键已经存在，更新该主键的值；  
    * 如果该主键不存在，则插件一对键值
    * @param keyname 键名  
    * @param keyvalue 键
    */   
    public static void setProperties(String keyname,String keyvalue) {          
        try {   
            OutputStream fos = new FileOutputStream(profilepath);   
            props.setProperty(keyname, keyvalue);   
            props.store(fos, "Update '" + keyname + "' value");   
        } catch (IOException e) {   
            System.err.println("属性文件更新错误");   
        }   
    }   
  
    /**  
    * 更新properties文件的键值对  
    */   
    public void updateProperties(String keyname,String keyvalue) {   
        try {   
            props.load(new FileInputStream(profilepath));   
            OutputStream fos = new FileOutputStream(profilepath);              
            props.setProperty(keyname, keyvalue);   
            props.store(fos, "Update '" + keyname + "' value");   
        } catch (IOException e) {   
        	 System.err.println("属性文件更新错误");   
        }   
    }   
    //测试代码   
    public static void main(String[] args) {   
    	/*getProertiesValue("access_token");
    	getProertiesValue("jsapi_ticket");  
    	getProertiesValue("update_time");
        setProperties("access_token", "23324");  
        setProperties("jsapi_ticket", "fff"); 
        setProperties("update_time", "2016-08-26 09:20:00"); 
        System.out.println("操作完成"); */ 
    	String update_time = getProertiesValue("update_time");
    	try  
    	{  
    	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    	    Date date = sdf.parse(update_time); 
    	    long updateTime = date.getTime();             //1472174419000
    	    long currentTime = System.currentTimeMillis();//1472011193444
    	    System.out.println("updateTime     "+updateTime);
    	    System.out.println("currentTime     "+currentTime +"      "+new Date());
    	    System.out.println("distance     "+(currentTime - updateTime));
    	    //1个小时5 分钟即为 6900000
    	    if((currentTime - updateTime)>6900000 ){
    	    	System.out.println("超过1个小时5 分钟");
    	    }else{
    	    	System.out.println("低于1个小时5 分钟");
    	    }
    	}  
    	catch (ParseException e)  
    	{  
    	    System.out.println(e.getMessage());  
    	}  
    }   
}   
